package com.youfang.sixthmedia.message.chat;

import java.util.ArrayList;
import java.util.Collections;

import org.json.JSONArray;
import org.json.JSONObject;

import com.youfang.sixthmedia.common.YoufangContext;
import com.youfang.sixthmedia.message.model.ChatItem;
import com.youfang.sixthmedia.net.Response;

/**
 * 
 * @author Administrator
 * 
 */
public class GetUnReadMsgResponse extends Response {

	private ArrayList<ChatItem> dataList = new ArrayList<ChatItem>();
	

	public boolean shouldShowLoginWhenNeedFailAuthed() {
		return false;
	}

	@Override
	protected void decodeBody(JSONObject bodyJSON) throws Exception {

		JSONArray data = bodyJSON.getJSONArray("data");
		int length = data.length();
		for (int i = 0; i < length; i++) {
			JSONObject o = data.getJSONObject(i);
			this.dataList.add(process(o));

		}

		Collections.sort(this.dataList);

	}

	private ChatItem process(JSONObject o) throws Exception {
		ChatItem item = new ChatItem();
		item.content = getString(o, "content");
		item.fromid = getInt(o, "fromid");
		item.fromnickname = getString(o, "fromnickname");
		item.fromusername = getString(o, "fromusername");
		item.messageid = getInt(o, "messageid");
		item.time = getLong(o, "time") * 1000;// from unix time to java time

		item.userid = YoufangContext.getUser().userid;

		return item;

	}

	public ArrayList<ChatItem> getDataList() {
		return dataList;
	}

	public void setDataList(ArrayList<ChatItem> dataList) {
		this.dataList = dataList;
	}

}
